<template>
  <div class="transport-management">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">运输管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新建运输任务
          </el-button>
        </div>
      </template>

      <!-- 统计卡片 -->
      <el-row :gutter="20" class="stats-row">
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <el-statistic title="运输中" :value="stats.shipping">
              <template #suffix>
                <span class="unit">单</span>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <el-statistic title="今日完成" :value="stats.todayCompleted">
              <template #suffix>
                <span class="unit">单</span>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <el-statistic title="异常运输" :value="stats.exception">
              <template #suffix>
                <span class="unit">单</span>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <el-statistic title="本月总运输" :value="stats.monthTotal">
              <template #suffix>
                <span class="unit">单</span>
              </template>
            </el-statistic>
          </el-card>
        </el-col>
      </el-row>

      <!-- 搜索区域 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="运输单号">
          <el-input v-model="searchForm.transportNo" placeholder="请输入运输单号" clearable />
        </el-form-item>
        <el-form-item label="车辆">
          <el-input v-model="searchForm.vehicle" placeholder="请输入车牌号" clearable />
        </el-form-item>
        <el-form-item label="司机">
          <el-input v-model="searchForm.driver" placeholder="请输入司机姓名" clearable />
        </el-form-item>
        <el-form-item label="运输状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待发车" value="waiting" />
            <el-option label="运输中" value="shipping" />
            <el-option label="已到达" value="arrived" />
            <el-option label="已完成" value="completed" />
            <el-option label="异常" value="exception" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table :data="tableData" v-loading="loading" stripe>
        <el-table-column prop="transportNo" label="运输单号" width="180" />
        <el-table-column prop="vehicle" label="车辆" width="120" />
        <el-table-column prop="driver" label="司机" width="100" />
        <el-table-column prop="driverPhone" label="司机电话" width="120" />
        <el-table-column prop="origin" label="始发地" width="150" />
        <el-table-column prop="destination" label="目的地" width="150" />
        <el-table-column prop="goodsCount" label="货物数量" width="100" align="right" />
        <el-table-column prop="distance" label="距离(km)" width="100" align="right" />
        <el-table-column prop="status" label="运输状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">{{ getStatusText(row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="departTime" label="发车时间" width="160" />
        <el-table-column prop="estimatedArrival" label="预计到达" width="160" />
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" size="small" @click="handleView(row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
            <el-button link type="primary" size="small" @click="handleTrack(row)">
              <el-icon><Position /></el-icon>
              追踪
            </el-button>
            <el-button link type="primary" size="small" @click="handleEdit(row)">
              <el-icon><Edit /></el-icon>
              编辑
            </el-button>
            <el-button link type="danger" size="small" @click="handleDelete(row)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.page"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="800px"
      @close="handleDialogClose"
    >
      <el-form :model="formData" :rules="rules" ref="formRef" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="车辆" prop="vehicle">
              <el-select v-model="formData.vehicle" placeholder="请选择车辆" filterable>
                <el-option label="京A12345" value="京A12345" />
                <el-option label="京B23456" value="京B23456" />
                <el-option label="京C34567" value="京C34567" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机" prop="driver">
              <el-select v-model="formData.driver" placeholder="请选择司机" filterable>
                <el-option label="张师傅" value="张师傅" />
                <el-option label="李师傅" value="李师傅" />
                <el-option label="王师傅" value="王师傅" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="司机电话" prop="driverPhone">
              <el-input v-model="formData.driverPhone" placeholder="请输入司机电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货物数量" prop="goodsCount">
              <el-input-number v-model="formData.goodsCount" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="始发地" prop="origin">
              <el-input v-model="formData.origin" placeholder="请输入始发地" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目的地" prop="destination">
              <el-input v-model="formData.destination" placeholder="请输入目的地" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="距离(km)" prop="distance">
              <el-input-number v-model="formData.distance" :min="0" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运输费用" prop="cost">
              <el-input-number v-model="formData.cost" :min="0" :precision="2" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="发车时间" prop="departTime">
              <el-date-picker
                v-model="formData.departTime"
                type="datetime"
                placeholder="选择发车时间"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计到达" prop="estimatedArrival">
              <el-date-picker
                v-model="formData.estimatedArrival"
                type="datetime"
                placeholder="选择预计到达时间"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="formData.remark" type="textarea" :rows="3" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  Plus, Search, Refresh, Edit, Delete, View, Position
} from '@element-plus/icons-vue'

// 统计数据
const stats = reactive({
  shipping: 45,
  todayCompleted: 23,
  exception: 3,
  monthTotal: 568
})

// 搜索表单
const searchForm = reactive({
  transportNo: '',
  vehicle: '',
  driver: '',
  status: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0
})

// 对话框
const dialogVisible = ref(false)
const dialogTitle = ref('新建运输任务')
const formRef = ref(null)

// 表单数据
const formData = reactive({
  vehicle: '',
  driver: '',
  driverPhone: '',
  goodsCount: 1,
  origin: '',
  destination: '',
  distance: 0,
  cost: 0,
  departTime: null,
  estimatedArrival: null,
  remark: ''
})

// 表单验证规则
const rules = {
  vehicle: [{ required: true, message: '请选择车辆', trigger: 'change' }],
  driver: [{ required: true, message: '请选择司机', trigger: 'change' }],
  driverPhone: [{ required: true, message: '请输入司机电话', trigger: 'blur' }],
  origin: [{ required: true, message: '请输入始发地', trigger: 'blur' }],
  destination: [{ required: true, message: '请输入目的地', trigger: 'blur' }],
  departTime: [{ required: true, message: '请选择发车时间', trigger: 'change' }]
}

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    waiting: 'info',
    shipping: 'primary',
    arrived: 'warning',
    completed: 'success',
    exception: 'danger'
  }
  return typeMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const textMap = {
    waiting: '待发车',
    shipping: '运输中',
    arrived: '已到达',
    completed: '已完成',
    exception: '异常'
  }
  return textMap[status] || status
}

// 加载数据
const loadData = () => {
  loading.value = true
  
  setTimeout(() => {
    tableData.value = [
      {
        id: 1,
        transportNo: 'TR2024010001',
        vehicle: '京A12345',
        driver: '张师傅',
        driverPhone: '13800138000',
        origin: '北京市朝阳区',
        destination: '上海市浦东新区',
        goodsCount: 150,
        distance: 1200,
        status: 'shipping',
        departTime: '2024-01-01 08:00:00',
        estimatedArrival: '2024-01-02 18:00:00'
      },
      {
        id: 2,
        transportNo: 'TR2024010002',
        vehicle: '京B23456',
        driver: '李师傅',
        driverPhone: '13900139000',
        origin: '广州市天河区',
        destination: '深圳市南山区',
        goodsCount: 200,
        distance: 150,
        status: 'arrived',
        departTime: '2024-01-02 09:00:00',
        estimatedArrival: '2024-01-02 12:00:00'
      }
    ]
    pagination.total = 2
    loading.value = false
  }, 500)
}

// 搜索
const handleSearch = () => {
  pagination.page = 1
  loadData()
}

// 重置
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新建运输任务'
  dialogVisible.value = true
}

// 查看详情
const handleView = (row) => {
  ElMessage.info(`查看运输任务: ${row.transportNo}`)
}

// 追踪
const handleTrack = (row) => {
  ElMessage.info(`追踪车辆: ${row.vehicle}`)
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑运输任务'
  Object.assign(formData, row)
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该运输任务吗？', '提示', {
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    loadData()
  })
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success(dialogTitle.value === '新建运输任务' ? '创建成功' : '更新成功')
      dialogVisible.value = false
      loadData()
    }
  })
}

// 对话框关闭
const handleDialogClose = () => {
  formRef.value?.resetFields()
}

// 分页改变
const handleSizeChange = () => {
  loadData()
}

const handleCurrentChange = () => {
  loadData()
}

onMounted(() => {
  loadData()
})
</script>

<style scoped lang="scss">
.transport-management {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      font-size: 16px;
      font-weight: 500;
    }
  }

  .stats-row {
    margin-bottom: 24px;

    .stat-card {
      :deep(.el-card__body) {
        padding: 20px;
      }

      .unit {
        font-size: 14px;
        color: #909399;
        margin-left: 4px;
      }
    }
  }

  .search-form {
    margin-bottom: 20px;
  }

  .pagination {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>

